using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace UDPPerformance
{
	public class Server : IDisposable
	{
		private readonly byte[] _payLoad = new byte[16000];
		private readonly List<Thread> _threads = new List<Thread>();

		public void Start()
		{
			int count = 4;
			while (count > 0)
			{
				Thread thread = new Thread(Run);
				_threads.Add(thread);
				thread.Start();
				count--;
			}

			Console.ReadLine();
			Dispose();
		}

		private void Run()
		{
			Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			EndPoint dest = new IPEndPoint(IPAddress.Parse("192.168.0.3"), 1234);

			for (int x = 0; x < 100000; x++)
			{
				socket.SendTo(_payLoad, dest);
			}
		}

		public void Dispose()
		{
			foreach (Thread thread in _threads)
			{
				thread.Abort();
				thread.Interrupt();
				thread.Join();
			}
		}

		~Server()
		{
			Dispose();
		}
	}
}
